package school.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import school.domain.Order;
import school.mapper.OrderMapper;
import school.support.BaseController;
import school.support.pojo.OrderPo;
import school.support.util.ParamUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;

@RequestMapping("order")
@Controller
public class OrderController extends BaseController {

    @Autowired
    private OrderMapper orderMapper;

    /**
     * 列表
     *
     * @param model
     * @param pageNum
     * @return
     */
    @RequestMapping("list")
    public String list(Model model, @RequestParam(defaultValue = "1") Integer pageNum, OrderPo orderPo) {
        Example example = new Example(Order.class);
        Example.Criteria criteria = example.createCriteria();
        if (orderPo.getTeachername() != null) {
            criteria.andCondition("u.name like ", "%" + orderPo.getTeachername() + "%");
        }
        if (orderPo.getDocname() != null) {
            criteria.andCondition("d.name like ", "%" + orderPo.getDocname() + "%");
        }
        if (orderPo.getDocno() != null) {
            criteria.andCondition("d.no like ", "%" + orderPo.getDocno() + "%");
        }
        PageHelper.startPage(pageNum, pageSize);
        PageInfo<OrderPo> pageInfo = new PageInfo<OrderPo>(orderMapper.selectPoByExample(example), 5);
        model.addAttribute(pageInfo);
        model.addAttribute("url", request.getRequestURI() + "?" + ParamUtils.params2String(request));
        return "order/list";
    }

    /**
     * 审批审核
     * @param order
     * @return
     */
    @RequestMapping("verify")
    public String verify(Order order){
        orderMapper.updateByPrimaryKeySelective(order);
        return refresh();
    }

    /**
     * 申请审批
     * @param id
     * @return
     */
    @RequestMapping("add/{id}")
    public String add(@PathVariable Integer id){
        Order order = new Order();
        order.setAddtime(new Date());
        order.setDocid(id);
        order.setUserid(sessionUser().getId());
        order.setStatus("待审批");
        orderMapper.insertSelective(order);
        return "redirect:/order/mylist";
    }

    /**
     * 我的申请
     * @param model
     * @param pageNum
     * @param orderPo
     * @return
     */
    @RequestMapping("mylist")
    public String mylist(Model model, @RequestParam(defaultValue = "1") Integer pageNum, OrderPo orderPo) {
        Example example = new Example(Order.class);
        Example.Criteria criteria = example.createCriteria();
        if (orderPo.getDocname() != null) {
            criteria.andCondition("d.name like ", "%" + orderPo.getDocname() + "%");
        }
        if (orderPo.getDocno() != null) {
            criteria.andCondition("d.no like ", "%" + orderPo.getDocno() + "%");
        }
        PageHelper.startPage(pageNum, pageSize);
        PageInfo<OrderPo> pageInfo = new PageInfo<OrderPo>(orderMapper.selectPoByExample(example), 5);
        model.addAttribute(pageInfo);
        model.addAttribute("url", request.getRequestURI() + "?" + ParamUtils.params2String(request));
        return "order/mylist";
    }
}
